﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using SourFruit.level.obj;

namespace SourFruit
{
    class Graphics
    {
        private Texture2D player;
        private int playerX, playerY, highlighted;
        private int collectedCounter;
        private Vector2 stats;
        private Dictionary<int, Tuple<Rectangle, Texture2D>> levelobjects;
        private Boolean menuOpen;
        private String[] menuTitles;

        /// <summary>
        /// Creates a object that handles all the game graphics.
        /// </summary>
        /// <param name="_player">The players starting texture. When the level starts this is the first player frame.</param>
        /// <param name="startMenu">The starting menu textures. When the game starts these are the first menu frames. </param>
        public Graphics(Texture2D _player, String[] startMenu)
        {
            this.player = _player;
            this.levelobjects = new Dictionary<int, Tuple<Rectangle, Texture2D>>();
            this.menuTitles = startMenu;
            playerX = 0;
            playerY = 0;
            collectedCounter = 0;
            stats = new Vector2(10, 10);
            menuOpen = true;
        }

        public void Draw(SpriteBatch sB, SpriteFont menufont)
        {
            if (menuOpen)
            {
                for (int i = 0; i < menuTitles.Length; i++)
                {
                    if (i == highlighted)
                        sB.DrawString(menufont, menuTitles[i], new Vector2(GameConfig.Instance.screenWidth / 2, GameConfig.Instance.screenHeight / 2 + menufont.MeasureString(menuTitles[i]).Y * i), Color.Red, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                    //sB.Draw(menuTextures[i], new Rectangle(GameConfig.screenWidth/2, GameConfig.screenHeight/2 + menuTextures[i].Height * i, menuTextures[i].Width, menuTextures[i].Height), null, Color.Red, 0f, new Vector2(0, 0), SpriteEffects.None, 0f);
                    else
                        sB.DrawString(menufont, menuTitles[i], new Vector2(GameConfig.Instance.screenWidth / 2, GameConfig.Instance.screenHeight / 2 + menufont.MeasureString(menuTitles[i]).Y * i), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                }
            }
            else
            {
                sB.DrawString(menufont, ""+collectedCounter, stats, Color.White);
                sB.Draw(player, new Rectangle(playerX, playerY, player.Width, player.Height), null, Color.Red, 0f, Vector2.Zero, SpriteEffects.None, 0f);
                foreach (var l in levelobjects)
                {
                    if (l.Value.Item1.X < 800 + l.Value.Item1.Width && l.Value.Item1.X > 0 - l.Value.Item1.Width)
                        sB.Draw(l.Value.Item2, l.Value.Item1, null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0f);
                }
            }
        }

        public void PlayerMoved(object sender, int x, int y, Texture2D texture)
        {
            playerX = x; playerY = y; player = texture; menuOpen = false;
        }
        
        public void ObjectMoved(object sender, int x, int y, int id, Texture2D texture)
        {
            int width = this.levelobjects[id].Item1.Width;
            int height = this.levelobjects[id].Item1.Height;
            this.levelobjects[id] = new Tuple<Rectangle, Texture2D>(new Rectangle(x, y, width, height), texture);
        }

        public void ObjectTextureChanged(int id, Texture2D texture)
        {
            int x = this.levelobjects[id].Item1.X;
            int y = this.levelobjects[id].Item1.Y;
            int width = this.levelobjects[id].Item1.Width;
            int height = this.levelobjects[id].Item1.Height;
            this.levelobjects[id] = new Tuple<Rectangle, Texture2D>(new Rectangle(x, y, width, height), texture);
        }

        public void addObject(int x, int y, int width, int height, int id, Texture2D texture)
        {
            this.levelobjects.Add(id, new Tuple<Rectangle,Texture2D> (new Rectangle(x, y, width, height), texture));
        }

        public void RemoveObject(int id, int value)
        {
            this.levelobjects.Remove(id);
            this.collectedCounter += value;
        }

        public void SetMenu(Boolean _menuOpen, String[] _menuTitles, int _highLighted)
        {
            menuOpen = _menuOpen;

            if (menuOpen)
            {
                menuTitles = _menuTitles;
                highlighted = _highLighted;
            }
        }
    }
}
